package businesslogic;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.SQLException;

import database.UserService;

import po.CustomerPO;
import po.UserPO;
import po.UserRole;

import businesslogicservice.CustomerBLService;

public class CustomerBL extends UnicastRemoteObject implements CustomerBLService{
	
	UserService userDatabase;
	
	public CustomerBL(String username, String password) throws RemoteException {
		
		try {
			userDatabase = new UserService(username, password);
		} catch (Exception e) {
			
		}
	}

	public void register(String username, String password, String name, String address, String phone) throws RemoteException,SQLException {
		CustomerPO customer = new CustomerPO(name, address, phone, 0, 0, 0);
		UserPO user = new UserPO(username, password, name, UserRole.CUSTOMER, customer);
		userDatabase.addUser(user);
	}

	public void withdraw(String username) throws RemoteException,SQLException {
		UserPO user = userDatabase.findUser(username);
		userDatabase.deleteUser(user);
	}

	public CustomerPO findCustomer(String username) throws RemoteException,SQLException {
		UserPO user = userDatabase.findUser(username);
		
		if(user==null || user.getCustomerPO() == null)
			return null;
		
		return user.getCustomerPO();
	}

}
